package com.bjim.sync;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjim.model.Dep;
import com.bjim.model.User;

import cn.dreampie.client.Client;
import cn.dreampie.client.ClientRequest;
import cn.dreampie.client.ClientResult;
import cn.dreampie.common.util.properties.Prop;
import cn.dreampie.common.util.properties.Proper;
import cn.dreampie.log.Logger;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class SyncUsers implements Job {
	static Logger logger = Logger.getLogger(SyncUsers.class);
	static Prop prop = Proper.use("application.properties");
	static String SyncStr="";
	/**
	 * 同步组织结构
	 */
	public String sync() {
		String msg="";
		logger.info("开始同步组织机构......");
		Client client = new Client(prop.get("syncurl"));
		ClientRequest request = new ClientRequest();
		request.setContentType("application/json");
		request.setJsonParam("{}");
		ClientResult getResult = client.build(request).post();
		String result = getResult.getResult();
		if(result.equals(SyncStr)){
			msg="获取的数据没有变化,不需要同步";
			logger.info(msg+":"+result);
			return result;
		}
		SyncStr=result;
		logger.info("同步json:"+result);
		JSONObject jo = JSONObject.parseObject(result);
		JSONArray rows = jo.getJSONArray("rows");
		for (int i = 0; i < rows.size(); i++) {
			JSONObject company = rows.getJSONObject(i);
			String companyId = company.getString("id");
			String companyCode = company.getString("companyCode");
			String companyNameZh = company.getString("companyNameZh");
			Dep company_dep = new Dep().set("depid", companyId).set("depcode", companyNameZh).set("depname",
					companyNameZh);
			if (company_dep.saveOrUpdate()) {
				logger.info("sync company successfully,companyId=%s,companyCode=%s,companyNameZh=%s", companyId,
						companyCode, companyNameZh);
			} else {
				logger.error("sync company error,companyId=%s,companyCode=%s,companyNameZh=%s", companyId, companyCode,
						companyNameZh);
			}
			JSONArray departments = company.getJSONArray("departments");
			if (departments == null)
				continue;
			for (int j = 0; j < departments.size(); j++) {
				JSONObject department = departments.getJSONObject(j);
				String depid = department.getString("id");
				String depcompanyId = department.getString("companyId");
				String dpartmentCode = department.getString("dpartmentCode");
				String departmentNameZh = department.getString("departmentNameZh");
				Dep dep = new Dep().set("depid", depid).set("depcode", dpartmentCode).set("depname", departmentNameZh)
						.set("parentdepid", depcompanyId);
				if (dep.saveOrUpdate()) {
					logger.info("sync dep successfully,depid=%s,dpartmentCode=%s,departmentNameZh=%s", depid,
							dpartmentCode, departmentNameZh);
				} else {
					logger.error("sync dep error,depid=%s,dpartmentCode=%s,departmentNameZh=%s", depid, dpartmentCode,
							departmentNameZh);
				}
				JSONArray users = department.getJSONArray("users");
				if (users == null)
					continue;
				logger.info("depid=%s  dep:%s   has  users "+users.size(),depid,departmentNameZh);
				for (int k = 0; k < users.size(); k++) {
					JSONObject user = users.getJSONObject(k);
					String userid = user.getString("id");
					String userMemo = user.getString("userMemo");
					JSONObject adUser = user.getJSONObject("adUser");
					JSONObject hrEmployee = user.getJSONObject("hrEmployee");
					if(adUser==null){
						logger.info("错误的信息:"+user.toJSONString());
						continue;
					}
					adUser.remove("memberOf");
					adUser.remove("objectClass");
					adUser.remove("groups");
					String username=adUser.getString("name");
					if(hrEmployee!=null){
						username=hrEmployee.getString("name");
					}
					JSONArray userExts = user.getJSONArray("userExts");
					for(int m=0;m<userExts.size();m++){
						userExts.getJSONObject(m).remove("page");
					}
					
					// String userid= user.getString("id");
					User user_m = new User().set("userid", userid).set("username", username).set("depid", depid);
					user_m.set("adUser", adUser.toJSONString());
					user_m.set("userExts", userExts==null?"":userExts.toJSONString());
					user_m.set("hrEmployee", hrEmployee==null?"":hrEmployee.toJSONString());
					logger.info("更新或保存:"+username);
					
					if (user_m.saveOrUpdate()) {
						logger.info("sync user successfully,userid=%s,userMemo=%s", userid, userMemo);
					} else {
						logger.error("sync user error, userid=%s,userMemo=%s", userid, userMemo);
					}
				}
			}
		}
		return result;
	}

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		//sync();
	}
}
